<template>
  <div class="login-container">
    <div class="login-header">
      <h2>欢迎回来</h2>
      <p class="login-subtitle">请输入您的账号密码登录系统</p>
    </div>

    <div class="login-form">
      <el-form ref="formRef" :model="user" :rules="rules">
        <el-form-item prop="username">
          <el-input 
            v-model="user.username" 
            type="text" 
            placeholder="用户名"
            prefix-icon="el-icon-user"
            size="large"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="user.password" 
            type="password" 
            placeholder="密码"
            prefix-icon="el-icon-lock"
            size="large"
            show-password
          />
        </el-form-item>
      </el-form>
    </div>

    <div class="login-actions">
      <el-button 
        type="primary" 
        size="large" 
        @click="signin"
        class="login-button"
      >
        立即登录
      </el-button>
    </div>

    <div class="login-footer">
      <p class="footer-text">© 2025 港口管理系统 | 技术支持</p>
    </div>
  </div>
</template>

<script>
import {ref} from "vue";
import {login} from '@/api/login'
import router from "@/router";
export default {
  setup(){
    const user = ref({
      username: '',
      password: '',
    })

    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度至少6位', trigger: 'blur' }
      ]
    }

    const signin = () => {
      login(user.value).then(res => {
        if (res.data.status == 200){
          router.replace('/company')
        } else {
          ElMessage.error(res.data.message || '登录失败')
        }
      }).catch(err => {
        ElMessage.error('网络错误或服务器异常')
      })
    }
    
    return {user, signin, rules}
  }
}
</script>

<style scoped>
.login-container {
  padding: 0 40px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-header {
  text-align: center;
  margin-bottom: 50px;
}

.login-header h2 {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 10px;
}

.login-subtitle {
  font-size: 14px;
  color: #909399;
}

.login-form {
  margin: 30px 0;
}

.login-form :deep(.el-input__inner) {
  height: 48px;
  line-height: 48px;
}

.login-form :deep(.el-input__prefix) {
  display: flex;
  align-items: center;
  padding-left: 10px;
}

.login-actions {
  margin-top: 30px;
  text-align: center;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border-radius: 4px;
}

.login-footer {
  position: absolute;
  bottom: 30px;
  left: 0;
  right: 0;
  text-align: center;
}

.footer-text {
  font-size: 12px;
  color: #C0C4CC;
}
</style>